﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DailyPoetryX.Models;
using DeferredEvents;

namespace DailyPoetryX.Services {
    /// <summary>
    /// 收藏存储。
    /// </summary>
    public interface IFavoriteStorage {
        /// <summary>
        /// 是否已经初始化。
        /// </summary>
        bool Initialized();

        /// <summary>
        /// 初始化。
        /// </summary>
        Task InitializeAsync();

        /// <summary>
        /// 获取一个收藏 。
        /// </summary>
        /// <param name="poetryId">诗词id。</param>
        Task<Favorite> GetFavoriteAsync(int poetryId);

        /// <summary>
        /// 获得所有收藏。
        /// </summary>
        Task<IList<Favorite>> GetFavoritesAsync();

        /// <summary>
        /// 所获得所有收藏项，包括收藏与非收藏。
        /// </summary>
        Task<IList<Favorite>> GetFavoriteItemsAsync();

        /// <summary>
        /// 保存收藏。
        /// </summary>
        /// <param name="favorite">待保存的收藏。</param>
        /// <param name="generateTimestamp">是否生成时间戳。</param>
        Task SaveFavoriteAsync(Favorite favorite, bool generateTimestamp);

        /// <summary>
        /// 收藏存储已更新事件。
        /// </summary>
        event EventHandler<FavoriteStorageUpdatedEventArgs> Updated;
    }

    public static class FavoriteStorageConstants {
        public const string VERSION_KEY =
            nameof(IFavoriteStorage) + "." + nameof(VERSION);

        public const int VERSION = 1;
    }

    public class FavoriteStorageUpdatedEventArgs : DeferredEventArgs {
        public Favorite UpdatedFavorite { get; }

        public FavoriteStorageUpdatedEventArgs(Favorite favorite) {
            UpdatedFavorite = favorite;
        }
    }
}